package cn.flying.cloud.message.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.base.common.utils.Ft;
import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.message.bo.MsgTemplateBo;
import cn.flying.cloud.message.entity.MsgTemplate;
import cn.flying.cloud.message.mapper.MsgTemplateMapper;
import cn.flying.cloud.message.service.MsgTemplateService;

/**
 * 消息模板表 服务实现类
 *
 * @author admin
 * @since 2025-09-24 10:42:07
 */
@Service
public class MsgTemplateServiceImpl extends ServiceImpl<MsgTemplateMapper, MsgTemplate> implements MsgTemplateService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 查询总数
     *
     * @param param
     * @return
     */
    @Override
    public long countObj(MsgTemplateBo param) {
        return this.count(getLambdaQueryWrapper(param));
    }

    /**
     * 分页查询消息模板表数据
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<MsgTemplateBo> getPageList(PageRequest<MsgTemplateBo> request) {
        MsgTemplateBo param = request.getParam();
        LambdaQueryWrapper<MsgTemplate> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        Page<MsgTemplate> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()), wrapper);
        return new PageResponse<>(page.getTotal(), (int) page.getCurrent(), page.getRecords().stream().map(MsgTemplate::toBO).collect(Collectors.toList()));
    }

    /**
     * 构建查询对象
     *
     * @param param
     * @return
     */
    private LambdaQueryWrapper<MsgTemplate> getLambdaQueryWrapper(MsgTemplateBo param) {
        LambdaQueryWrapper<MsgTemplate> wrapper = new LambdaQueryWrapper<>();
        if (param != null) {
            if (Ft.String.isNotBlank(param.getTenantId())) {
                wrapper.eq(MsgTemplate::getTenantId, param.getTenantId());
            }
            if (Ft.String.isNotBlank(param.getServiceCode())) {
                wrapper.eq(MsgTemplate::getServiceCode, param.getServiceCode());
            }
            if (Ft.String.isNotBlank(param.getMode())) {
                wrapper.eq(MsgTemplate::getMode, param.getMode());
            }
            if (Ft.String.isNotBlank(param.getTplName())) {
                wrapper.likeLeft(MsgTemplate::getTplName, param.getTplName());
            }
            if (Ft.String.isNotBlank(param.getTplId())) {
                wrapper.eq(MsgTemplate::getTplId, param.getTplId());
            }
            if (Ft.String.isNotBlank(param.getStatus())) {
                wrapper.eq(MsgTemplate::getStatus, param.getStatus());
            }
        }
        return wrapper;
    }

    /**
     * 列表查询消息模板表数据
     *
     * @param param
     * @return
     */
    @Override
    public List<MsgTemplateBo> getList(MsgTemplateBo param) {
        LambdaQueryWrapper<MsgTemplate> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        List<MsgTemplate> list = this.list(wrapper);
        return list.stream().map(MsgTemplate::toBO).collect(Collectors.toList());
    }

    /**
     * 查询消息模板表详情数据
     *
     * @param param
     * @return
     */
    @Override
    public MsgTemplateBo getOneObj(MsgTemplateBo param) {
        LambdaQueryWrapper<MsgTemplate> wrapper = getLambdaQueryWrapper(param);
        if (wrapper.isEmptyOfWhere()) {
            return new MsgTemplateBo();
        }
        return this.getOne(wrapper).toBO();
    }

    /**
     * 根据Id查询消息模板表详情数据
     *
     * @param id
     * @return
     */
    @Override
    public MsgTemplateBo getObjById(Serializable id) {
        return this.getById(id).toBO();
    }

    /**
     * 新增消息模板表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createObj(MsgTemplateBo param) {
        param.setTenantId(Ft.Local.TenantId());
        MsgTemplate obj = new MsgTemplate();
        obj.fromBO(param);
        return this.save(obj);
    }

    /**
     * 更新消息模板表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateObj(MsgTemplateBo param) {
        MsgTemplate obj = new MsgTemplate();
        obj.fromBO(param);
        return this.saveOrUpdate(obj);
    }

    /**
     * 删除消息模板表数据
     *
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteObj(MsgTemplateBo param) {
        LambdaQueryWrapper<MsgTemplate> wrapper = getLambdaQueryWrapper(param);
        // wrapper TODO
        return this.remove(wrapper);
    }

    /**
     * 根据Id删除消息模板表数据
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Serializable id) {
        return this.removeById(id);
    }
}
